package com.erp.reciver;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.erp.module.crm.xXCrm.ClientKActivity;
import com.erp.module.dingDan.prodyctionClient.ProdyctionClientActivity;
import com.erp.contacts.Contacts;
import com.erp.notifiactionDb.NotificationBean;
import com.erp.notifiactionDb.PersonDB;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/26/026.
 * 作者：刘洋
 */
public class Myrecevicer extends MessageReceiver {
    /**标识将要跳转的页面
     //     * 1表示客户
     //     * 2表示订单
     //     * 3表示其他
     //     **/
    private int identification=3;
    /**想要打开客户或者订单的oidid*/
    private String oidid="";
    private PersonDB personDB;
    @Override
    protected void onNotificationOpened(final Context context, String s, String s1, final String s2) {
        super.onNotificationOpened(context, s, s1, s2);
        final int noticationid = 0;
        if(isForeground(context,"com.erp")){
            runintent(context, s2, noticationid);
        }else {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                            runintent(context, s2, noticationid);
                    }

                }, 4000);
        }
    }
    private boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className)) {
            return false;
        }

        ActivityManager __am = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = __am.getRunningAppProcesses();
        if(list.size() == 0) return false;
        for(ActivityManager.RunningAppProcessInfo process:list)
        {
            if(process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                    process.processName.equals(className))
            {
                return true;
            }
        }
        return false;
    }
    private void runintent(Context context, String s2, int noticationid) {
        try{
            JSONObject jo=new JSONObject(s2);
            noticationid=jo.getInt("_ALIYUN_NOTIFICATION_ID_");
        }catch (JSONException e){
            e.printStackTrace();
        }
        personDB = PersonDB.getIntance();
        List<NotificationBean> notificationBeens=personDB.findnotification(noticationid);
        if(notificationBeens!=null){
            NotificationBean notificationBean=notificationBeens.get(0);
            notificationBean.setIstrue(true);
            personDB.updata(notificationBean);
            if(notificationBean.getIdentification()==1){
                Intent intent=new Intent(context,ClientKActivity.class);
                intent.putExtra("id",notificationBean.getOidid());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }else if(notificationBean.getIdentification()==2){
                Intent intent=new Intent(context,ProdyctionClientActivity.class);
                intent.putExtra("id",notificationBean.getOidid());
                intent.putExtra("selsct",0);
                intent.putExtra("all","");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        }
        Intent intent1 = new Intent();
        intent1.setAction(Contacts.BORADCAST_ACTION_NOTI);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.sendBroadcast(intent1);
    }

    @Override
    protected void onNotification(Context context, String s, String s1, Map<String, String> map) {
        super.onNotification(context, s, s1, map);
        Intent intent1 = new Intent();
        String time="0";
        Log.d("TAAAG",map.toString());
        Iterator keys = map.keySet().iterator();
        while(keys.hasNext()){
            String key = (String)keys.next();
            if("crm_order".equals(key)){
                try {
                    JSONObject jo=new JSONObject(map.get("crm_order"));
                    if(jo.has("crm_oid")){
                        identification=1;
                        oidid=jo.getString("crm_oid");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if("order".equals(key)){
                try {
                    JSONObject jo=new JSONObject(map.get("order"));
                    if(jo.has("oid")){
                        identification=2;
                        oidid=jo.getString("oid");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if("ctime".equals(key)){
                time=map.get("ctime");
            }
            if("update".equals(key)){
                if(map.get("update").equals("true")){
                        intent1.setAction(Contacts.BORADCAST_ACTION_UPDATE);
                        context.sendBroadcast(intent1);
                    return;
                }
            }
        }
        personDB = PersonDB.getIntance();
        NotificationBean person = new NotificationBean(s,s1,time,identification,oidid,false,Integer.valueOf(map.get("_ALIYUN_NOTIFICATION_ID_")));
        personDB.savePerson(person);
        intent1.setAction(Contacts.BORADCAST_ACTION_NOTI);
        context.sendBroadcast(intent1);
    }

    @Override
    protected void onMessage(Context context, CPushMessage cPushMessage) {
        super.onMessage(context, cPushMessage);
    }
}
